$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/transaction/getAnalysisList',
        datatype: "json",
        colModel: [
            { label: '日期', name: 'createtime', index: 'createtime', width: 80 },
			{ label: '订单量', name: 'orderCount', index: 'orderCount', width: 80,formatter: function(cellvalue, options, rowdata) {
				if(rowdata.orderCount == null ||rowdata.orderCount == ''){
					return 0;
				}else{
					return rowdata.orderCount;
				}
			} },
			{ label: '商品数量', name: 'productCount', index: 'productCount', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.productCount == null ||rowdata.productCount == ''){
                        return 0;
                    }else{
                        return rowdata.productCount;
                    }
                }},
			{ label: '订单金额', name: 'orderMoney', index: 'orderMoney', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.orderMoney == null ||rowdata.orderMoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.orderMoney;
                    }
                } },
			{ label: '优惠券金额', name: 'couponMoney', index: 'couponMoney', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.couponMoney == null ||rowdata.couponMoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.couponMoney;
                    }
                }},
			{ label: '支付金额', name: 'payMoney', index: 'payMoney', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.payMoney == null ||rowdata.payMoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.payMoney;
                    }
                } },
            { label: '平均订单价', name: 'avgOrderPrice', index: 'avgOrderPrice', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.avgOrderPrice == null ||rowdata.avgOrderPrice == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.avgOrderPrice;
                    }
                } }

        ],
		viewrecords: true,
        height: 385,
        rownumbers: true,
        rowNum: -1,
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        jsonReader : {
            root: "list"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.getCharImgList();
            vm.chartImg();
        }
    });
    vm.Statistics(null,null,null);
    vm.queryGoodshelves();


});

var vm = new Vue({
	el:'#rrapp',
	data:{

		showList: true,
		title: null,
		transaction: {},
		xArray:[],
        payArray:[],
        ordercountArray:[]
	},
	methods: {
		query: function () {
            var sheelvesid =  $("#sheelves_id").val();
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            vm.reload(sheelvesid,startime,endtime);
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.transaction = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        Statistics: function (shelvesid,startime,endtime) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/transaction/infoStatistics",
                data:{"shelvesid":shelvesid,"startime":startime,"endtime":endtime},
                success: function(r){
                	var orderCount = 0;
                	var productCount = 0;
                	var payMoney = 0;
                	var avgOrderPrice = 0;
                	if(r.transaction !=null){
                        orderCount = r.transaction.orderCount;
                        productCount = r.transaction.productCount;
                        payMoney = r.transaction.payMoney;
                        avgOrderPrice = r.transaction.avgOrderPrice;
					}
                    $("#ddl").text(orderCount);
                    $("#spsl").text(productCount);
                    $("#zfje").text("￥"+payMoney);
                    $("#pjddj").text("￥"+avgOrderPrice);
                }
            });
        },
        getCharImgList: function () {
            //拿到grid对象
            var obj = $('#jqGrid');
            //获取grid表中所有的rowid值
            var rowIds = obj.getDataIDs();
            var arrayData = new Array();
            if (rowIds.length > 0) {
                for (var i = 0; i < rowIds.length; i++) {
                    arrayData.push(obj.getRowData(rowIds[i]));
                }
            }
            vm.xArray = [];
            vm.payArray = [];
            vm.ordercountArray = [];
            for(var i=0;i<arrayData.length;i++){
                vm.xArray.push(arrayData[i].createtime);
                vm.payArray.push(parseInt(arrayData[i].payMoney.substring(1)));
                vm.ordercountArray.push(parseInt(arrayData[i].orderCount));
            }
        },
		chartImg:function(){
            var chart = Highcharts.chart('container', {
                chart: {
                    zoomType: 'xy'
                },
                title: {
                    text: '交易分析'
                },
                xAxis: [{
                    categories: vm.xArray,
                    crosshair: true
                }],
                yAxis: [{
                    labels: {
                        format: '{value}',
                        style: {
                            color: Highcharts.getOptions().colors[1]
                        }
                    },
                    title: {
                        text: '订单数量',
                        style: {
                            color: Highcharts.getOptions().colors[1]
                        }
                    }
                }, {
                    title: {
                        text: '支付金额',
                        style: {
                            color: Highcharts.getOptions().colors[0]
                        }
                    },
                    labels: {
                        format: '{value} ',
                        style: {
                            color: Highcharts.getOptions().colors[0]
                        }
                    },
                    opposite: true
                }],
                tooltip: {
                    shared: true
                },
                legend: {
                    layout: '订单数量',
                    align: 'left',
                    x: 120,
                    verticalAlign: 'top',
                    y: 100,
                    floating: true,
                    backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
                },
                series: [{
                    name: '支付金额',
                    type: 'column',
                    yAxis: 1,
                    data: vm.payArray

                }, {
                    name: '订单数量',
                    type: 'spline',
                    data: vm.ordercountArray
                }],
                credits: {
                    enabled: false     //不显示LOGO
                },
                exporting: {
                    enabled:false
                }
            });
		},
		setDate: function (num) {
            var mydate = new Date();
            var year = mydate.getFullYear(); //获取完整的年份(4位,1970-????)
            var month   = mydate.getMonth()+1; //获取当前月份(0-11,0代表1月)
            if(month<10){
                month = "0"+month;
            }
            var day = mydate.getDate(); //获取当前日(1-31)
            if(day<10){
                day = "0"+day;
            }

            var preDate = new Date(mydate.getTime() - (24*60*60*1000*num));

            if(day == 0){
                preDate = mydate;
			}
            var preyear = preDate.getFullYear(); //获取完整的年份(4位,1970-????)
            var premonth   = preDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
            if(premonth<10){
                premonth = "0"+premonth;
            }
            var preday = preDate.getDate(); //获取当前日(1-31)
            if(preday<10){
                preday = "0"+preday;
            }

            $("#startime").val(preyear+"-"+premonth+"-"+preday);
            $("#endtime").val(year+"-"+month+"-"+day);
        },
		saveOrUpdate: function (event) {
			var url = vm.transaction.id == null ? "sys/transaction/save" : "sys/transaction/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.transaction),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/transaction/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/transaction/info/"+id, function(r){
                vm.transaction = r.transaction;
            });
		},
        queryGoodshelves: function(){
            $("#sheelves_id").html("");
            $.get(baseURL + "sys/goodshelves/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].shelvesname+"</option>";

                });
                $("#sheelves_id").append(temp);

            });
        },
		reload: function (sheelvesid,startime,endtime) {
			vm.showList = true;
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    shelvesid:sheelvesid,
                    startime:startime,
                    endtime:endtime
				},
                gridComplete:function(){
                    vm.Statistics(sheelvesid,startime,endtime);
                    vm.getCharImgList();
                    vm.chartImg();
                }

            }).trigger("reloadGrid");
		}
	}
});